package com.atguigu.day07;

public class Example8 {

    public static void main(String[] args) {
        Counter counter = new Counter();
        Thread t1 = new Thread(counter, "Thread-1");
        Thread t2 = new Thread(counter, "Thread-2");
        Thread t3 = new Thread(counter, "Thread-3");
        t1.start();
        t2.start();
        t3.start();
    }

    public static class Counter implements Runnable {
        private int number = 0;
        @Override
        public void run() {
            for (int i = 0; i < 1000000; i++) {
                // 1. 将number中的值由内存取出，放入寄存器
                // 2. 在寄存器中将number加一
                // 3. 将寄存器中的number写回内存
                synchronized (this) {
                    number = number + 1;
                }
            }
            System.out.println(number);
        }
    }
}
